package com.fqgj.sentry.policy.dao.impl;

import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.sentry.policy.dao.PcExecutorDao;
import com.fqgj.sentry.policy.entity.PcExecutorEntity;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * User: yujianfu
 * Date: 2017-07-12
 * Time: 09:37:58.115
 */

@Repository
public class PcExecutorDaoImpl extends AbstractBaseMapper<PcExecutorEntity> implements PcExecutorDao {

    @Override
    public PcExecutorEntity insertExecutor(PcExecutorEntity pcExecutorEntity) {
        pcExecutorEntity.setDeleted(false);
        pcExecutorEntity.setGmtCreate(new Date());
        pcExecutorEntity.setGmtModified(new Date());
        getSqlSession().insert(getStatement("insertExecutor"), pcExecutorEntity);
        return pcExecutorEntity;
    }

    @Override
    public List<PcExecutorEntity> selectListByPolicySetId(Long policySetsId) {
        return getSqlSession().selectList(getStatement("selectListByPolicySetId"), policySetsId);
    }

    @Override
    public PcExecutorEntity selectPcExecutorCurrent(Long policySetsId, Integer executorType, Integer active) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("policySetsId", policySetsId);
        param.put("type", executorType);
        param.put("active", Arrays.asList(0, 1).contains(active) ? active : null);
        return getSqlSession().selectOne(getStatement("selectPcExecutorCurrent"), param);
    }

    @Override
    public List<PcExecutorEntity> selectPcExecutorHistoryList(Long policySetsId, Integer type) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("policySetsId", policySetsId);
        param.put("type", type);
        return getSqlSession().selectList(getStatement("selectPcExecutorHistoryList"), param);
    }

    @Override
    public PcExecutorEntity selectPcExecutorPending(Long policySetsId, Integer type) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("policySetsId", policySetsId);
        param.put("type", type);
        return getSqlSession().selectOne(getStatement("selectPcExecutorPending"), param);
    }

    @Override
    public Integer updateExecutorToHistory(Long policySetsId, Integer type) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("policySetsId", policySetsId);
        param.put("type", type);
        param.put("modifiedDate", new Date());
        return Integer.valueOf(getSqlSession().update(getStatement("updateExecutorToHistory"), param));
    }

    @Override
    public void deleteByPoliceSetId(Long policySetId) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("policySetsId", policySetId);
        getSqlSession().selectList(getStatement("deleteByPoliceSetId"), param);
    }
}